<template>
  <div class="search-container">
    <!-- 搜索栏 -->
    <!--
      Tips: 在 van-search 外层增加 form 标签，且 action 不为空，即可在 iOS 输入法中显示搜索按钮
     -->
    <form action="/">
      <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索关键词"
        background="#3296fa"
        @search="onSearch(searchText)"
        @cancel="onCancel"
        @focus="isResultShow = false"
      />
    </form>
    <!-- /搜索栏 -->

    <!-- 搜索结果 -->
    <search-result v-if="isResultShow" :search-text='searchText'/>
    <!-- /搜索历史记录 -->

    <!-- 联想建议 -->
    <search-suggestion 
      v-else-if="searchText" 
      :search-text='searchText' 
      @search="onSearch"
    />
    <!-- /联想建议 -->

    <!-- 历史记录 -->
    <search-history v-else 
      :search-histories="searchHistories"
      @search="onSearch"
      @update-histories="searchHistories = $event"
     />
    <!-- /历史记录 -->
  </div>
</template>

<script>
import SearchHistory from './components/search-history'
import SearchSuggestion from './components/search-suggestion'
import SearchResult from './components/search-result'
import {setItem,getItem} from '@/utils/storage'
// import {getSearchHistories} from '@/api/search'
import {mapState} from 'vuex'

export default {
  name: 'SearchIndex',
  components: {
    SearchHistory,
    SearchSuggestion,
    SearchResult
  },
  props: {},
  data () {
    return {
      searchText: '', //搜索输入框的内容
      isResultShow: false,//控制搜索结果的显示状态
      searchHistories:[], //搜索历史记录
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {
    //监视搜索历史记录的变化,存储到本地存储
    searchHistories () {
      // 同步到本地存储
      setItem('search-histories', this.searchHistories)
    }
  },
  created () {
    this.loadSearchHistories()
  },
  mounted () {},
  methods: {
    onSearch (searchText) {
      this.searchText = searchText

      //  indexOf找到匹配字符的索引
      const index = this.searchHistories.indexOf(searchText)
      if (index!==-1) {
        //把重复项删除
        this.searchHistories.splice(index,1)
      }

      //把最新的搜索历史记录放到顶部
      this.searchHistories.unshift(searchText)

      //如果用户已登录，则把搜索历史记录存储在线上
      //提示：只要我们调用获取搜索结果的数据接口，后端会给我们自动存储用户的搜索历史记录
      //如果没有登录页，则把搜索历史记录存储到本地

      //setItem('search-histories',this.searchHistories) //index已经监听了setItem

      //展示搜索结果
      this.isResultShow=true
    },
    onCancel () {
      this.$router.back()
    },
    async loadSearchHistories(){
      //因为后端帮我们存储的用户搜索历史记录太少了（只有4条）
      //所以我们这里让后端返回的历史记录和本地的历史记录合并到一起
      //如果用户已登录
      const searchHistories = getItem('search-histories') || []

      // if (this.user) {
      //   const {data} = await getSearchHistories()
      //   //console.log(data.data.keywords);
      //   //数组合并：[...数组，...数组] 
      //   //把set转换为数组：[...set对象]
      //   //数据去重：[...new Set([...数组,...数组])]
      //   searchHistories = [...new Set([ 
      //     ...searchHistories,
      //     ...data.data.keywords
      //   ])]
      // }

      //console.log(searchHistories);
      //return searchHistories
      this.searchHistories = searchHistories
    }
  }
}
</script>

<style scoped lang="less">
.search-container {
  .van-search__action {
    color: #fff;
  }
}
</style>